package com.mmall.product.controller;

import com.mmall.common.base.BooleanResult;
import com.mmall.common.base.PageResult;
import com.mmall.common.base.ServerResponse;
import com.mmall.pojo.param.AddProductParam;
import com.mmall.pojo.param.ProductPageParam;
import com.mmall.pojo.param.UpdateProductParam;
import com.mmall.pojo.vo.ProductVo;
import com.mmall.product.service.ProductService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 商品
 * <p>
 * 商品管理不同于sku管理 商品是sku的父级
 *
 * @author: gg
 * @date: 2018/12/10 20:04
 */
@RestController
@RequestMapping("/product/")
public class ProductController {

    @Resource
    private ProductService productService;


    /**
     * 添加商品
     *
     * @param addProductParam AddProductParam
     * @return ServerResponse
     */
    @PostMapping("add")
    public ServerResponse add(@Valid @RequestBody AddProductParam addProductParam) {
        ProductVo productVo = productService.add(addProductParam);
        return productVo == null ? ServerResponse.createByError() : ServerResponse.createBySuccess(productVo);
    }

    /**
     * 添加商品
     *
     * @param updateProductParam UpdateProductParam
     * @return ServerResponse
     */
    @PostMapping("update")
    public ServerResponse update(@Valid @RequestBody UpdateProductParam updateProductParam) {
        ProductVo productVo = productService.update(updateProductParam);
        return productVo == null ? ServerResponse.updateError() : ServerResponse.updateSuccess(productVo);
    }

    /**
     * 分页列表 后台管理
     *
     * @param productPageParam ProductPageParam
     * @return ServerResponse
     */
    @GetMapping("page")
    public ServerResponse page(ProductPageParam productPageParam) {
        ProductPageParam.initPage(productPageParam);
        PageResult pageResult = productService.page(productPageParam);
        return pageResult == null ? ServerResponse.createByError() : ServerResponse.createBySuccess(pageResult);
    }

    /**
     * 商品详情
     *
     * @param id 商品id
     * @return ServerResponse ItemVo 单个对象
     */
    @GetMapping("detail/{id}")
    public ServerResponse detail(@PathVariable Long id) {
        ProductVo productVo = productService.detail(id);
        return productVo == null ? ServerResponse.createError() : ServerResponse.createBySuccess(productVo);
    }

    /**
     * 删除商品
     *
     * @param id 商品id
     * @return ServerResponse ItemVo 单个对象
     */
    @GetMapping("del/{id}")
    public ServerResponse del(@PathVariable Long id) {
        BooleanResult result = productService.del(id);
        return result.getResult() ? ServerResponse.deleteSuccess() : ServerResponse.deleteError();
    }
}
